package com.example.autumn.service.impl;

import com.baomidou.mybatisplus.annotations.DataSource;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.example.autumn.entity.User;
import com.example.autumn.mapper.UserMapper;
import com.example.autumn.service.IUserService;
import com.example.common.response.R;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Liao run
 * @since 2022-08-31
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    @Override
    @DataSource(name = "mo-yu-ucenter-1")
    public R<User> getUserById(Long id) {
        User user = baseMapper.selectById(id);

        return R.SUCCESS("查询成功", user);
    }

    /**
     * 用户注册
     * 这个方法调用的其他的微服务的分支事务都由 seata 管理
     *
     * @param user
     * @return
     */
    @Override
//    @GlobalTransactional    // seata + sharding jdbc 的分表策略太复杂了
    @Transactional(rollbackFor = Exception.class)
    public R register(User user) {
        boolean b = this.insert(user);

        return b ? R.SUCCESS("ok") : R.Error();
    }
}
